package org.example.pmsystem.service.Impl;

import org.example.pmsystem.entity.Staff;
import org.example.pmsystem.entity.User;
import org.example.pmsystem.entity.UserRole;
import org.example.pmsystem.mapper.SalaryMapper;
import org.example.pmsystem.mapper.UserMapper;
import org.example.pmsystem.mapper.StaffMapper;
import org.example.pmsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StaffMapper staffMapper;

    // 登录
    @Override
    public String login(User user){
        User tmp = userMapper.selectUserByuserid(user.getUserid());
        if (tmp == null) return "该用户不存在";
        if (!tmp.getPsw().equals(user.getPsw()))
            return "密码错误";
        return "success";
    }

    @Override
    public User getUserRoleByuserid(String userid) {
        return userMapper.getUserRoleByuserid(userid);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public boolean insertUser(User user) {
        // 可能存在 账号重复 无法插入 的问题
        if (userMapper.selectUserByuserid(user.getUserid()) != null)
            return false;
        userMapper.insertUser(user);
        Staff staff = user.getStaff();
        if (staff == null) return true;

        // 员工信息添加
        staff.setSNo(user.getUserid());
        staffMapper.insertStaff(staff);
        UserRole ur = new UserRole();
        ur.setUserid(user.getUserid());
        ur.setRoleId(7);
        userMapper.insertUserRole(ur);
        return true;
    }

    @Override
    public boolean insertUserRole(User user)
    {
        UserRole ur = new UserRole();
        ur.setUserid(user.getUserid());
        for (int i : user.getRoleList()){
            ur.setRoleId(i);
            if (userMapper.checkUserRole(ur)>0)
                continue;
            userMapper.insertUserRole(ur);
        }
        return true;
    }

    // 只会修改/删除已存在的账号，不需要判断
    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
        Staff staff = user.getStaff();
        if (staff == null) return;
        staff.setSNo(user.getUserid());
        staffMapper.updateStaff(staff);
    }

    // 删除账号，同时删除该账号的员工信息、角色
    @Override
    public void deleteUserByuserid(String userid) {
        List<Integer> list = userMapper.getUserRoleByuserid(userid).getRoleList();
        if (list.size() > 0){
            // 删除用户角色
//            User user =new User();
//            user.setUserid(userid);
//            user.setRoleList(list);
//            userMapper.deleteUserRole(user);
            UserRole ur = new UserRole();
            ur.setUserid(userid);
            for (int i: list){
                ur.setRoleId(i);
                userMapper.deleteUserRole(ur);
            }
        }

        // 有外键，必须先删除staff再删除user
//        System.out.println(staffMapper.checkStaffBySno(userid));
        if (staffMapper.checkStaffBySno(userid) > 0)
            staffMapper.deleteStaffBySno(userid);
        userMapper.deleteUserByuserid(userid);
    }

    @Override
    public boolean deleteUserRole(User user){
//        userMapper.deleteUserRole(user);
        UserRole ur = new UserRole();
        ur.setUserid(user.getUserid());
        for (int i : user.getRoleList()){
            ur.setRoleId(i);
//            if (userMapper.checkUserRole(ur)>0)
//                continue;
            userMapper.deleteUserRole(ur);
        }
        return true;
    }
}
